import * as vscode from 'vscode';
import { SnippetManager } from '../managers/SnippetManager';
import { Snippet } from '../types/snippet';

export class DragDropProvider implements vscode.TreeDragAndDropController<Snippet> {
  // 新增接口要求属性
  dragMimeTypes = ['application/vnd.code.tree.snippets'];
  dropMimeTypes = ['application/vnd.code.tree.snippets'];
  
  private _onDidChangeTreeData = new vscode.EventEmitter<void>();
  
  constructor(private snippetManager: SnippetManager) {}

  async handleDrag(source: Snippet[], treeDataTransfer: vscode.DataTransfer) {
    treeDataTransfer.set(this.dragMimeTypes[0], 
      new vscode.DataTransferItem(source));
  }

  async handleDrop(target: Snippet|undefined, treeDataTransfer: vscode.DataTransfer) {
    const transferItem = treeDataTransfer.get(this.dropMimeTypes[0]);
    if (!transferItem) {
      return;
    }

    const draggedItems: Snippet[] = transferItem.value;
    await this.snippetManager.reorderSnippets(draggedItems, target?.id);
    this._onDidChangeTreeData.fire();
  }
}
